<template>
  <div class="app-container">
     <!-- 类型选择 -->
    <div style="margin-bottom:10px;">
      <el-dropdown trigger="click" @command="handleSelectType">
          <el-button type="primary">
            {{ selectType }} <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="item in articlesTypes" :command="item">{{item}}</el-dropdown-item>
          </el-dropdown-menu>
      </el-dropdown>
    </div>
    <!-- 标题 -->
    <div style="margin:10px 0;padding:0px;">
      <el-input v-model="articleTitle" placeholder="请输入文章标题" style="margin:0px;padding:30px auto !important; width:800px;"></el-input>
    </div>
    <!-- 编辑器 -->
    <div style="border: 1px solid #ccc;">
        <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
        />
        <Editor
            style="height: 500px; overflow-y: hidden;"
            v-model="html"
            :defaultConfig="editorConfig"
            :mode="mode"
            @onCreated="onCreated"
        />
    </div>
    <div style="padding:10px;">
      <el-button type="primary" @click="submitData" size="medium" v-hasPermi="['system:articles:add']">提交</el-button>
  </div>
  </div>
</template>
<script>
import Vue from 'vue'
import { DomEditor } from '@wangeditor/editor'
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import { listArticles, getArticles, delArticles, addArticles, updateArticles } from "@/api/system/articles";
export default {
  name: 'MyEditor',
    components: { Editor, Toolbar },
  data() {
    return {
      //编辑器
      editor: null,
      html: '',
      toolbarConfig: { 
        // toolbarKeys :[
        //       // 菜单 key
        //       'headerSelect',

        //       // 分割线
        //       '|',

        //       // 菜单 key
        //       'bold', 'italic',

        //       // 菜单组，包含多个菜单
        //       {
        //           key: 'group-more-style', // 必填，要以 group 开头
        //           title: '更多样式', // 必填
        //           iconSvg: '<svg>....</svg>', // 可选
        //           menuKeys: ["through", "code", "clearStyle"] // 下级菜单 key ，必填
        //       },
        //       // 继续配置其他菜单...
        // ]
        excludeKeys:[ //排除 
            'imageWidth30',
            'imageWidth50',
            'imageWidth100',
            'codeBlock',
            // 'insertImage',
            'uploadImage',
            'editImage',
            'deleteImage',
            'viewImageLink',
            'viewLink',
            'insertVideo',
            'uploadVideo'
        ]
      },
      editorConfig: { placeholder: '请输入内容...' },
      mode: 'simple', // or 'simple or default
      updateid:null,
      selectType:'选择发布类型',
      articlesTypes:["课程介绍","收费明细","优惠活动"],
      //文章标题
      articleTitle:'', 
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      articlesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        atype: null,
        atitle: null,
        bodydata: null,
        createtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        atype: [
          { required: true, message: "文章类型/课程介绍/收费明细不能为空", trigger: "change" }
        ],
      }
    };
  },
  beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
  },
  created() {
    this.getList();
    console.log("路由id",this.$route.query.id);
    if(this.$route.query.id!=null){
      this.updateid=this.$route.query.id;
      this.getArticlesInfo(this.updateid);
    }
  },
  mounted() {

  },
  methods: {
    getArticlesInfo(id){
      getArticles(id).then(res=>{
          console.log("返现数据信息:",res);
          if(res.code===200){
            this.articleTitle=res.data.atitle;
            this.selectType=res.data.atype;
            this.editor.setHtml(res.data.bodydata);
          }
      })
    },
    handleSelectType(command){
      this.selectType=command;
    },
    submitData(){
      if(this.selectType==='选择发布类型' || this.selectType==='' || this.selectType===' '){
        this.$alert('请选择发布类型', '提示', {});
        return;
      }
      if(this.articleTitle==='' || this.articleTitle===' '){
        this.$alert('请输入标题', '提示', {});
        return;
      }
      // if(this.editor.getText()==='' ||this.editor.getText()===' '){
      //   this.$alert('文章内容不能为空', '提示', {});
      //   return;
      // }
      //获取的话，是获取html格式的文章内容
      console.log(this.editor.getHtml());

      this.form.atype=this.selectType;
      this.form.atitle=this.articleTitle;
      this.form.bodydata=this.editor.getHtml();

      console.log("上传的参数:",this.form);
      if(this.updateid!=null){
          this.form.id=this.updateid;
          updateArticles(this.form).then(response => {
              this.$modal.msgSuccess("内容已更新");
              this.open = false;
              this.getList();
            });
      }else{
          addArticles(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                //this.getList();
          });
      }

    },
    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
      //查看菜单key
      const toolbar = DomEditor.getToolbar(this.editor);
      console.log("查看toolbar菜单设置",this.editor.getConfig().MENU_CONF);
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listArticles(this.queryParams).then(response => {
        this.articlesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        atype: null,
        atitle: null,
        bodydata: null,
        createtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticles(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticles(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticles(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delArticles(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/articles/export', {
        ...this.queryParams
      }, `articles_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>